<?php   // $Id: convert_client.php 347 2012-05-07 09:11:55Z pcoustillas $ ?>
<?php   
/*Copyright (C) 2003-2004 Guy Hendrickx
Licensed under the terms of the GNU  General Public License:http://www.opensource.org/licenses/gpl-license.php
File Authors:Guy Hendrickx
Modification : Pierre Coustillas*/

$type_verif="devis"; //pour vérifier que l'utisateur à le droit de valider ce devis
$num_doc=$_POST['num_dev']; //pour vérifier que l'utisateur à le droit de valider ce devis
require_once(dirname(__FILE__) ."/verif_client.php");
require_once("../include/config/common.php");
require_once("../include/config/var.php");
require_once("../include/language/$lang.php");


$num_dev=isset($_POST['num_dev'])?$_POST['num_dev']:"";
$login=isset($_POST['login'])?$_POST['login']:"";
$message=isset($_POST['message'])?$_POST['message']:"";
$jour = date("d");
$mois = date("m");
$annee = date("Y");

//on recpere les donnÃ©e de devis
$sql0 = "SELECT * FROM ${tblpref}devis WHERE num_dev=$num_dev";
$req = mysql_query($sql0) or die("Erreur selection du devis !<br/>$sql0<br/>" . mysql_error());

$data = mysql_fetch_array($req);

$num_dev = $data['num_dev'];
$client_num = $data['client_num'];
$date = $data['date'];
$tot_htva = $data['tot_htva'];
$tot_tva = $data['tot_tva'];

//on les reinjecte dans la base bon_comm
$sql1 = "INSERT INTO ${tblpref}bon_comm ( client_num, date, tot_htva, tot_tva ) VALUES ( $client_num, '$annee-$mois-$jour', $tot_htva, $tot_tva )";
mysql_query($sql1) or die("Erreur création du bon de commande !<br/>$sql1<br/>" . mysql_error());

//passer le devis Ã  l'etat ok
$sql2 = "UPDATE ${tblpref}devis SET resu='ok' WHERE num_dev=$num_dev";
mysql_query($sql2) or die("Erreur mise à jour du devis !<br/>$sql2<br/>" . mysql_error());

//touver le dernier enregistrement pour le numero de bon
$sql = "SELECT MAX(num_bon) As Maxi FROM ${tblpref}bon_comm";
$result = mysql_query($sql) or die('Erreur');
$max = mysql_result($result, 0);

$sql3 = "SELECT * FROM ${tblpref}cont_dev WHERE dev_num=$num_dev";
$req = mysql_query($sql3) or die("Erreur selection du contenu devis !<br/>$sql3<br/>" . mysql_error());

//Il faut recréer toutes les lignes de bon 
while($data = mysql_fetch_array($req))
{

	$article_num = $data['article_num'];
	$quanti = $data['quanti'];
	$tot_art_htva = $data['tot_art_htva'];
	$to_tva_art = $data['to_tva_art'];
	
		
	$sql4 = "INSERT INTO ${tblpref}cont_bon (bon_num, article_num, quanti, tot_art_htva, to_tva_art) 
		VALUES ('$max', '$article_num', '$quanti', '$tot_art_htva', '$to_tva_art')";
	mysql_query($sql4) or die("Erreur création des lignes de devis !<br/>$sql4<br/>" . mysql_error());
}

/*
//a verifier
$from = "$mail" ;
$to = "$mail";
$subject = "devis$lang_de_num $num_dev $lang_con_cli" ;
//$message =  ""; 
$header = 'From: '.$from."\n"
 .'MIME-Version: 1.0'."\n"
 .'Content-Type: text/html; charset= ISO-8859-1'."\n"
 .'Content-Transfer-Encoding: 7bit'."\n\n";

mail($to,$subject,$message,$header);
*/
$msg="Devis convertit en commande avec succés";
header("Location: client.php?msg=$msg");

?>